package com.groupseven.happycampus.common.utils;

import com.groupseven.happycampus.common.pojo.Card;
import com.groupseven.happycampus.common.pojo.CardPayLog;
import com.groupseven.happycampus.common.pojo.CardRechargeLog;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @BeLongsProject: happycampus
 * @BeLongsPackage: com.groupseven.happycampus.common.utils
 * @Author: WRH
 * @CreateTime: 2022-07-26 16:50
 * @Description: TODO
 * @Version: 1.0
 */
public class BeanUtils {

    /**
     * @description: 实现将 Map 中的数据注入到 Card 对象中
     * @author: WRH
     * @date: 2022/7/26 17:24
     * @param: [params, card]
     * @return: void
     **/
    public static void copyOf(Map<String, Object> params,Card card) {
        if (params.get("cardId")!= null){
            card.setCardId(Integer.parseInt((String) params.get("cardId")) );
        }
        if (params.get("userName")!= null){
            card.setUserName((String) params.get("userName"));
        }
        if (params.get("userId")!= null){
            card.setUserId((String) params.get("userId"));
        }
        if(params.get("cardState")!= null){
            card.setCardState((String) params.get("cardState"));
        }
        if("".equals((String) params.get("cardBalance"))){
            card.setCardBalance(new BigDecimal("0.00"));
        }else {
            card.setCardBalance(new BigDecimal((String) params.get("cardBalance")));
        }
        if(!"".equals((String) params.get("cardFreezeTime"))){
            card.setCardFreezeTime(dateFormat((String) params.get("cardFreezeTime")));
        }

    }

    public static void copyOf(Map<String, Object> params, CardPayLog cardPayLog) {
        if(params.get("cardId")!= null){
            cardPayLog.setCardId(Integer.parseInt((String) params.get("cardId")) );
        }
        if(params.get("payId")!= null){
            cardPayLog.setPayId(Integer.parseInt((String) params.get("payId")));
        }
        if(params.get("payType")!= null){
            cardPayLog.setPayType((String) params.get("payType"));
        }
        if(params.get("payMoney")!= null){
            if("".equals((String) params.get("payMoney"))){
                cardPayLog.setPayMoney(new BigDecimal("0.00"));
            }else {
                cardPayLog.setPayMoney(new BigDecimal((String) params.get("payMoney")));
            }
        }
        if(params.get("payDate")!= null){
            if(!"".equals((String) params.get("payDate"))){
                cardPayLog.setPayDate(dateFormat((String) params.get("payDate")));
            }
        }

    }
    public static void copyOf(Map<String, Object> params, CardRechargeLog cardRechargeLog) {
        if(params.get("cardId")!= null){
            cardRechargeLog.setCardId(Integer.parseInt((String) params.get("cardId")) );
        }
        if(params.get("rechargeId")!= null){
            cardRechargeLog.setRechargeId(Integer.parseInt((String) params.get("rechargeId")));
        }
        if(params.get("cardNote")!= null){
            cardRechargeLog.setCardNote((String) params.get("cardNote"));
        }
        if(params.get("rechargeMoney")!= null){
            if("".equals((String) params.get("rechargeMoney"))){
                cardRechargeLog.setRechargeMoney(new BigDecimal("0.00"));
            }else {
                cardRechargeLog.setRechargeMoney(new BigDecimal((String) params.get("rechargeMoney")));
            }
        }
        if(params.get("rechargeDate")!= null){
            if(!"".equals((String) params.get("rechargeDate"))){
                cardRechargeLog.setRechargeDate(dateFormat((String) params.get("rechargeDate")));
            }
        }

    }

    /**
     * @description: 初始化日期格式
     * @author: WRH
     * @date: 2022/7/26 21:32
     * @param: [datetime]
     * @return: java.util.Date
     **/

    public static Date dateFormat(String datetime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            return sdf.parse(datetime);
        }catch (ParseException e){
            e.printStackTrace();
        }
        return null;
    }
}
